#include <iostream>
#include <string>

using std::cout;
using std::endl;
using std::string;

int gNumber = 1234;

void test()
{
    string name("wangdao");
    int age = 100;
    int number = 20;

    [name, age](int value)
    {
        cout << "value = " << value << endl;
        cout << "name = " << name << endl;
        cout << "age = " << age << endl;
        cout << "gNumber = " << gNumber << endl;
    }(300);

    cout << endl;
    //将外部变量全部以值传递的形式捕获到lambda表达式中
    [=](int value)
    {
        cout << "value = " << value << endl;
        cout << "name = " << name << endl;
        cout << "age = " << age << endl;
        cout << "number = " << number << endl;
    }(300);

    cout << endl;
    /* [&name, &age, &number](int value) */
    //将外部变量全部以引用传递的形式捕获到lambda表达式中(可以修改)
    [&](int value)
    {
        name = "lili";
        age = 200;
        number = 400;
        cout << "value = " << value << endl;
        cout << "name = " << name << endl;
        cout << "age = " << age << endl;
        cout << "number = " << number << endl;
    }(300);
    cout << endl;
    cout << "name = " << name << endl;
    cout << "age = " << age << endl;

    cout << endl;
    //name是值传递，但是age与number是引用传递
    /* [name, &age, &number](int value) */
    //(name是值传递，其他外部变量是引用传递)
    /* [&, name](int value) */
    [&, name](int value)
    {
        age = 111;
        number = 222;
        cout << "value = " << value << endl;
        cout << "name = " << name << endl;
        cout << "age = " << age << endl;
        cout << "number = " << number << endl;
    }(400);

    cout << endl;
    [=, &name](int value)
    {
        name = "beijing";
        cout << "value = " << value << endl;
        cout << "name = " << name << endl;
        cout << "age = " << age << endl;
        cout << "number = " << number << endl;
    }(400);
}

int main(int argc, char *argv[])
{
    test();
    return 0;
}

